package wh.ispct

class UserController {
	
   def listJson(){
		def msg = ["success":true]
		try{
			def limit = 25
			def start = 0
			if(params.limit) limit = params.limit.toInteger()
			if(params.start) start = params.start.toInteger()
			
			msg["data"] = User.findAll("from User as u order by u.role desc")
			msg["total"] = User.count()
		}catch(e){
			msg.put("success", false)
			msg.put("msg", e.getMessage())
		}
		render  (contentType:"text/json"){ msg }
	}
   
   def save(){
	   def msg = ["success":true]
	   try{
		   User u = params.id ? User.load(params.id) : new User()
		   bindData(u ,params)
		   u.save()
	   }catch(e){
		   msg.put("success", false)
		   msg.put("msg", e.getMessage())
	   }
	   render  (contentType:"text/json"){
		   msg
	   }
   }
   
   def delete(){
	   def msg = ["success":true]
	   try{
		   User u = params.id ? User.get(params.id) : new User()
		   if(u)
		   	u.delete()
	   }catch(e){
		   msg.put("success", false)
		   msg.put("msg", e.getMessage())
	   }
	   render  (contentType:"text/json"){
		   msg
	   }
   }
   
   def setPwd(){
	   def msg = ["success":true]
	   try{
		   User u = User.load(params.id) 
		   u.pwd = params.pwd
		   u.save()
	   }catch(e){
		   msg.put("success", false)
		   msg.put("msg", e.getMessage())
	   }
	   render  (contentType:"text/json"){
		   msg
	   }
   }
}
